package com.ruoyi.web.controller.front;

import java.util.List;

import com.ruoyi.common.utils.UUIDFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.front.domain.SBanner;
import com.ruoyi.front.service.ISBannerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 首页轮播图Controller
 * 
 * @author jyp
 * @date 2021-07-22
 */
@Controller
@RequestMapping("/front/banner")
public class SBannerController extends BaseController
{
    private String prefix = "front/banner";

    @Autowired
    private ISBannerService sBannerService;

    @RequiresPermissions("front:banner:view")
    @GetMapping()
    public String banner()
    {
        return prefix + "/banner";
    }

    /**
     * 查询首页轮播图列表
     */
    @RequiresPermissions("front:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SBanner sBanner)
    {
        startPage();
        List<SBanner> list = sBannerService.selectSBannerList(sBanner);
        return getDataTable(list);
    }

    /**
     * 导出首页轮播图列表
     */
    @RequiresPermissions("front:banner:export")
    @Log(title = "首页轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SBanner sBanner)
    {
        List<SBanner> list = sBannerService.selectSBannerList(sBanner);
        ExcelUtil<SBanner> util = new ExcelUtil<SBanner>(SBanner.class);
        return util.exportExcel(list, "banner");
    }

    /**
     * 新增首页轮播图
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存首页轮播图
     */
    @RequiresPermissions("front:banner:add")
    @Log(title = "首页轮播图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SBanner sBanner)
    {
        if(sBanner.getType() != 1){
            sBanner.setSort(0);
        }
        sBanner.setId(UUIDFactory.getStringId());
        return toAjax(sBannerService.insertSBanner(sBanner));
    }

    /**
     * 修改首页轮播图
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        SBanner sBanner = sBannerService.selectSBannerById(id);
        mmap.put("sBanner", sBanner);
        return prefix + "/edit";
    }

    /**
     * 修改保存首页轮播图
     */
    @RequiresPermissions("front:banner:edit")
    @Log(title = "首页轮播图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SBanner sBanner)
    {
        return toAjax(sBannerService.updateSBanner(sBanner));
    }

    /**
     * 删除首页轮播图
     */
    @RequiresPermissions("front:banner:remove")
    @Log(title = "首页轮播图", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sBannerService.deleteSBannerByIds(ids));
    }
}
